/**
 * Created by qismsom on 2018/7/31.
 */

export function getNow() {
  let date =  new Date();
  return date;
}

export function formatDate(now) {
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let date = now.getDate();
  let hour = now.getHours();
  let minute = now.getMinutes();
  let second = now.getSeconds();
  if(month<10){
    month = '0'+month
  }
  return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
};

//   console.log('时间戳转时间');

export function timesToDate(time) {
  let date =  new Date(time);
  let y = 1900+date.getYear();
  let m = "0"+(date.getMonth()+1);
  let d = "0"+date.getDate();
  return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
}

export function yyyyddFormatDate(date) {
  if (date === null || date === '') {
    return '';
  }
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  if(month<10){
    month = '0'+month
  }
  return year + "-" + month;
}

// 转换成  yyyy-mm-dd格式
export function yyyymmddFormatDate(date) {
  if (date === null || date === '') {
    return '';
  }
  let y = date.getFullYear();
  let m = "0"+(date.getMonth()+1);
  let d = "0"+date.getDate();
  return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
}

// 转换成  某年某月1号格式
export function yyyymm01FormatDate(date) {
  if (date === null || date === '') {
    return '';
  }
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  if(month<10){
    month = '0'+month
  }
  return year + "-" + month + "-01";
}

export function getFirstDay(date) {
  let year = date.getFullYear();
  let month = date.getMonth()+1;
  if (month < 10) {
    month = "0" + month;
  }
  let firstDay = year + "-" + month + "-" + "01";
  console.log('第一天');
  console.log(firstDay);
  return firstDay;
}

export function getLastDay(date) {
  let year = date.getFullYear();
  let month = date.getMonth()+1;
  if (month < 10) {
    month = "0" + month;
  }
  let myDate = new Date(year, month, 0);
  let lastDay = year + "-" + month + "-" + myDate.getDate();
  return lastDay;
}



export function formatDate_hs (now) {
  now = new Date(new Date(now).getTime());
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let date = now.getDate();
  let hour = now.getHours();
  let minute = now.getMinutes();
  let second = now.getSeconds();
  if(month<10){
    month = '0'+month
  }
  if(date<10) {
    date = '0'+date
  }
  if(hour<10){
    hour = '0'+hour
  }
  if(minute<10){
    minute = '0'+minute
  }
  if(second<10){
    second = '0'+second
  }
  return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

/**
 * 字符串转Data格式
 * @param strDate
 * @returns {Date}
 */
export  function strToDate(strDate) {
   let s = strDate.replace(/-/g,"/");
   return new Date(s );
}

export function FormatAllDate(sDate) {
  var date = new Date(sDate);
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  //月
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  //日
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  //时
  if (hours >= 0 && hours <= 9) {
    hours = "0" + hours;
  }
  //分
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  //秒
  if (seconds >= 0 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  //格式化后日期为：yyyy-MM-dd HH:mm:ss
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + " " + hours + seperator2 + minutes + seperator2 + seconds;
  return currentdate;
}

/**
 * 日期转时间
 * @param sDate
 * @returns {string}
 * @constructor
 */
export function FormatTimeDate(sDate) {
  var date = new Date(sDate);
  var seperator2 = ":";
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  //时
  if (hours >= 0 && hours <= 9) {
    hours = "0" + hours;
  }
  //分
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  //秒
  if (seconds >= 0 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  //格式化后日期为：yyyy-MM-dd HH:mm:ss
  var currentdate = hours + seperator2 + minutes + seperator2 + seconds;
  return currentdate;
}

export function getChineseStringToNow(timestamp){
  var mistiming=Math.round(new Date() / 1000)-timestamp;
  var arrr = ['年','个月','星期','天','小时','分钟','秒'];
  var arrn = [31536000,2592000,604800,86400,3600,60,1];
  for(var i=6;i>=0;i--){
    var inm = Math.floor(mistiming/arrn[i]);
    if(inm!=0){
      return inm+arrr[i]+'前';
    }
  }
}


